package cn.zhaoguang.usedcar.main.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.zhaoguang.usedcar.main.entity.Carousel;
import cn.zhaoguang.usedcar.main.entity.dto.CarouselDTO;
import cn.zhaoguang.usedcar.main.entity.vo.CarouselVO;
import cn.zhaoguang.usedcar.main.mapper.CarouselMapper;
import cn.zhaoguang.usedcar.main.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author zhaoguang
 * @Date 2021/7/7 10:33
 */
@Service
public class CarouselServiceImpl implements CarouselService {

    @Autowired
    private CarouselMapper carouselMapper;

    @Override
    public int add(Carousel carousel) {
        Integer exist = carouselMapper.findExistByUsedcarId(carousel.getUsedcarId());
        if (exist != null) {
            //已经存在了，返回0
            return 0;
        }
        carousel.setOperationTime(DateUtil.date());
        return carouselMapper.insertSelective(carousel);
    }

    @Override
    public int delete(Integer id) {
        return carouselMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<CarouselVO> findAll() {
        List<CarouselDTO> list = carouselMapper.findAll();
        //处理集合数据
        List<CarouselVO> carouselVO = list.stream().map(u -> {
            CarouselVO carouse = new CarouselVO();
            String name = String.format("%s %s %s %s %s %s", u.getCarBrand(), u.getCarSeries(), u.getCarYear(), u.getCarDisplacement(), u.getCarVersion(), u.getTransmission());
            carouse.setUsedcarTitleName(name);

            BeanUtil.copyProperties(u, carouse);
            return carouse;
        }).collect(Collectors.toList());

        return carouselVO;
    }
}
